package com.hjy.config;


import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class LoginInterceptor implements HandlerInterceptor {

    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        Object username = request.getSession().getAttribute("username");
        if (username==null){
            request.setAttribute("message","您未登录,权限不够");
            //注意,由于templates下的文件不可以直接访问,所以我们要通过服务器内部的请求再转发过去
            //由于配置了registry.addViewController("/index.html").setViewName("index");,所以我们可以直接转发到这个请求,再转发到页面
            request.getRequestDispatcher("/index.html").forward(request,response);
            return false;
        }else {
            return true;
        }
    }
}
